/**
 * 中控类 —— 家电控制命令集
 *
 * @author  <XueYang.Yan@bshg.com>
 * @createTime   <2018.12.5>
 * @version <1.0.0>
 *
 */
/**
 * TT —— 锅炉采暖温度设定值
 */
export const TT_SETTING_BlCHSet = 'SmartAssistant.TT.Setting.BlCHSet'

/**
 * TT —— 锅炉采暖温度值
 */
export const TT_SETTING_BlCHWt = 'SmartAssistant.TT.Setting.BlCHWt'

/**
 * TT —— 锅炉采暖温度设定最大值
 */
export const TT_SETTING_BlCHMAX = 'SmartAssistant.TT.Setting.BlCHMax'

/**
 * TT —— 锅炉生活热水设定值
 */
export const TT_SETTING_BlDHWSet = 'SmartAssistant.TT.Setting.BlDHWSet'

/**
 * TT —— 锅炉生活热水设定最大值
 */
export const TT_SETTING_BlDHWMax = 'SmartAssistant.TT.Setting.BlDHWMax'

/**
 * TT —— 锅炉生活热水出水温度
 */
export const TT_SETTING_BlDHWWt = 'SmartAssistant.TT.Setting.BlDHWWt'

/**
 * TT —— 锅炉状态字段
 */
export const TT_SETTING_BlState = 'SmartAssistant.TT.Setting.BlState'

/**
 * TT —— 锅炉能耗字段
 */
export const TT_SETTING_BlGasEnergy = 'SmartAssistant.TT.Setting.BlGasEnergy'

/**
 * TT —— 锅炉采暖水压
 */
export const TT_SETTING_BlWtPressure = 'SmartAssistant.TT.Setting.BlWtPressure'

/**
 * TT —— 锅炉防冻状态
 */
export const TT_SETTING_BLANTIFROZEN = 'SmartAssistant.TT.Setting.BlAntiFrozen'

/**
 * TT —— 锅炉火焰状态
 */
export const TT_SETTING_BLFLAME = 'SmartAssistant.TT.Setting.BlFlame'

/**
 * TT —— 控制器错误码
 */
export const TT_SETTING_BlErrCode = 'SmartAssistant.TT.Setting.BlErrCode'

/**
 * TT —— 控制器错误码BlErrStr
 */
export const TT_SETTING_BlErrStr = 'SmartAssistant.TT.Setting.BlErrStr'

/**
 * TT的OTA升级标识
 */
export const TT_STATUS_MCUSTATUS = 'SmartAssistant.TT.Status.MCUStatus'
export const TT_STATUS_MCUSTATUS_OFF = 'SmartAssistant.TT.Status.MCUStatus.Off'
export const TT_STATUS_MCUSTATUS_DOWNLOAD = 'SmartAssistant.TT.Status.MCUStatus.Download'
export const TT_STATUS_MCUSTATUS_UPGRADE = 'SmartAssistant.TT.Status.MCUStatus.Upgrade'

/**
 * 天气补偿算法开关-MR120
 */
export const TT_SETTING_WDC_MR120 = 'SmartAssistant.TT.Setting.BlWDC'

/**
 * TT —— 锅炉配置字段
 */
export const TT_SETTING_BlConfig = 'SmartAssistant.TT.Setting.BlConfig'

/**
 * 获取夏季模式
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const getTTSummerMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let summerMode = binaryValue.substring(7, 8)
  return parseInt(summerMode, 2)
}

/**
 * 设置夏季模式
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const setTTSummerMode = (value: string, changeValue: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  binaryValue = binaryValue.substr(0, 7) + changeValue

  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}

/**
 * 获取采暖调节模式自动/手动
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const getTTAutoMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let summerMode = binaryValue.substring(6, 7)
  return parseInt(summerMode, 2)
}

/**
 * 获取锅炉加热状态
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const getTTFlameState = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let flameState = binaryValue.substring(5, 6)
  return parseInt(flameState, 2)
}

/**
 * 获取锅炉加热状态
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const getTTBLMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let flameState = binaryValue.substring(6, 7)
  return parseInt(flameState, 2)
}

/**
 * 设置锅炉加热状态
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const setTTBLMode = (value: string, changeValue: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  binaryValue = binaryValue.substr(0, 6) + changeValue + binaryValue.substr(7, 8)

  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}

/**
 * 获取房间工作模式
 * 房间配置字段 8bit
 * 76543210
 * bit[0:3]=工作模式(0=自动, 1=手动, 2=在家, 3=睡眠, 4=离家, 5=待机)
 * bit4=编程模式1天4段(0)/2天4段(1)
 * @param value 房间配置字段
 *
 */
export const getTTRoomMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let flameState = binaryValue.substring(4, 8)
  return parseInt(flameState, 2)
}

/**
 * 获取房间计时模式
 * 房间配置字段 8bit
 * 76543210
 * bit[0:3]=工作模式(0=自动, 1=手动, 2=在家, 3=睡眠, 4=离家, 5=待机)
 * bit4=编程模式1天4段(0)/2天4段(1)
 * @param value 房间配置字段
 *
 */
export const getTTRoomTmMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let flameState = binaryValue.substring(3, 4)
  return parseInt(flameState, 2)
}

/**
 * 获取房间编程模式
 * 房间配置字段 8bit
 * 76543210
 * bit[0:3]=工作模式(0=自动, 1=手动, 2=在家, 3=睡眠, 4=离家, 5=待机)
 * bit4=编程模式1天4段(0)/2天4段(1)
 * @param value 房间配置字段
 *
 */
export const getTTProgramMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let flameState = binaryValue.substring(3, 4)
  return parseInt(flameState, 2)
}
/**
 * 设置房间工作模式
 * 房间配置字段 8bit
 * 76543210
 * bit[0:3]=工作模式(0=自动, 1=手动, 2=在家, 3=睡眠, 4=离家, 5=待机)
 * bit4=编程模式1天4段(0)/2天4段(1)
 * @param value 房间配置字段
 *
 */
export const setTTRoomMode = (value: string, changeValue: string) => {
  let binaryValue = parseInt(value).toString(2)
  let binaryChangeValue = parseInt(changeValue).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  needZeroPaddingLength = 4 - binaryChangeValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryChangeValue = zeroPadding + binaryChangeValue
  }
  binaryValue = binaryValue.substr(0, 4) + binaryChangeValue
  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}

/**
 * 设置编程模式
 * 房间配置字段 8bit
 * 76543210
 * bit[0:3]=工作模式(0=自动, 1=手动, 2=在家, 3=睡眠, 4=离家, 5=待机)
 * bit4=编程模式1天4段(0)/2天4段(1)
 * @param value 房间配置字段
 *
 */
export const setTTProgramMode = (value: string, changeValue: number) => {
  if (changeValue == 36) {
    changeValue = 1
  } else {
    changeValue = 0
  }
  let binaryValue = parseInt(value).toString(2)
  let binaryChangeValue = changeValue.toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  binaryValue = binaryValue.substr(0, 3) + binaryChangeValue + '0000'
  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}
/**
 * 设置编程数据对应工作日
 * 房间状态字段
 * 76543210
 * bit0=水阀状态关(0)/开(1)，
 * bit1=房间预热模式关(0)/开(1)，
 * bit2=当前编程数据对应工作日(0)/周末(1)
 * @param value 房间配置字段
 *
 */
export const getRoomValve = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let flameState = binaryValue.substring(7, 8)
  return parseInt(flameState, 2)
}

/**
 * 设置编程数据对应工作日
 * 房间状态字段
 * 76543210
 * bit0=水阀状态关(0)/开(1)，
 * bit1=房间预热模式关(0)/开(1)，
 * bit2=当前编程数据对应工作日(0)/周末(1)
 * @param value 房间配置字段
 *
 */
export const setTTProgramDay = (value: string, changeValue: number) => {
  if (changeValue == 2) {
    changeValue = 1
  } else {
    changeValue = 0
  }
  let binaryValue = parseInt(value).toString(2)
  let binaryChangeValue = changeValue.toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  needZeroPaddingLength = 4 - binaryChangeValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryChangeValue = zeroPadding + binaryChangeValue
  }
  binaryValue = binaryValue.substr(0, 5) + binaryChangeValue + binaryValue.substr(6, 8)
  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}

/**
 * 获取房间预热模式关
 * 房间状态字段
 * 76543210
 * bit0=水阀状态关(0)/开(1)，
 * bit1=房间预热模式关(0)/开(1)，
 * bit2=当前编程数据对应工作日(0)/周末(1)
 * @param value 房间配置字段
 *
 */
export const getRoomPreheat = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let flameState = binaryValue.substring(6, 7)
  return parseInt(flameState, 2)
}

/**
 * 设置房间预热模式关
 * 房间状态字段
 * 76543210
 * bit0=水阀状态关(0)/开(1)，
 * bit1=房间预热模式关(0)/开(1)，
 * bit2=当前编程数据对应工作日(0)/周末(1)
 * @param value 房间配置字段
 *
 */
export const setRoomPreheat = (value: string, changeValue: string) => {
  let binaryValue = parseInt(value).toString(2)
  let binaryChangeValue = parseInt(changeValue).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  needZeroPaddingLength = 4 - binaryChangeValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryChangeValue = zeroPadding + binaryChangeValue
  }
  binaryValue = binaryValue.substr(0, 6) + binaryChangeValue + binaryValue.substr(7, 8)
  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}

/**
 * 获取防冻状态
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const getTTAntiFrozenMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let summerMode = binaryValue.substring(4, 5)
  return parseInt(summerMode, 2)
}
/**
 * 设置防冻状态
 * 锅炉状态字段 8bit
 * 76543210
 * 00011000
 * 00001000
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const setTTAntiFrozenMode = (value: string, changeValue: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  binaryValue = binaryValue.substr(0, 4) + changeValue + binaryValue.substr(5, 8)

  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}

/**
 * 获取温度补偿WDC状态
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const getTTWDCMode = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let summerMode = binaryValue.substring(3, 4)
  return parseInt(summerMode, 2)
}
/**
 * 设置温度补偿WDC状态
 * 锅炉状态字段 8bit
 * 76543210
 * bit0=冬(0)/夏(1)
 * bit1=采暖调节自动(0)/手动(1)
 * bit2=火焰熄灭(0)/燃烧(1)
 * bit3=防冻关闭(0)/开启(1)
 * bit4=算法普通(0)/温度补偿WDC(1)
 * bit5=获取云端天气结果失败 (0)/成功(1)
 * @param value 锅炉状态字段
 *
 */
export const setTTWDCMode = (value: string, changeValue: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  binaryValue = binaryValue.substr(0, 3) + changeValue + binaryValue.substr(4, 8)

  console.log('binaryValue--1', binaryValue)
  return parseInt(binaryValue, 2)
}
/**
 * 获取锅炉类型 是否为单采暖
 * 锅炉配置字段，
 * bit0=CH设定值只读(0)/可写(1)，
 * bit1=DHW设定值只读(0)/可写(1)，
 * bit2=耗气量不支持(0)/支持(1)，
 * bit3=DHW泵编程模式1天24段(0)/2天24段(1)，
 * bit4=错误码保持(0)/消除(1)，
 * bit5=TBD，
 * bit6=外置水泵支持(1)/不支持(0)
 * @param value 锅炉状态字段
 *
 */
export const getTTBoilerType = (value: string) => {
  let binaryValue = parseInt(value).toString(2)
  let zeroPadding = '0'
  console.log('binaryValue', binaryValue.length)
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let summerMode = binaryValue.substring(6, 7)
  return parseInt(summerMode, 2)
}
/**
 * 获取Bl_ErrStr
 * @param value 锅炉错误码字符串
 */
export const getTTBlErrStrKey = (value: string) => {
  let binaryValue = parseInt(value).toString(16)
  let zeroPadding = '0'
  let needZeroPaddingLength = 8 - binaryValue.length
  for (var i = 0; i < needZeroPaddingLength; i++) {
    binaryValue = zeroPadding + binaryValue
  }
  let newNinaryValue = [...binaryValue]
  let newValue = []
  for (let j = 0; j < newNinaryValue.length; j++) {
    if ((j + 1) % 2 == 0) {
      let str = newNinaryValue[j - 1] + newNinaryValue[j] //两个两个为一组
      let num = parseInt(str, 16) //16进制转为10进制
      let ascllNum = String.fromCharCode(num) //转为Ascll码
      newValue.unshift(ascllNum) //小端模式
    }
  }
  let result = newValue.join('')
  return result.trim()
}
